var selected = "newNode";
		var divs = ["newNode","swapNode","updateNode","newFlow","delNode"];
        function getOps(name){
            var element = document.getElementById("svg1");
            var svg = element.getSVGDocument();
            if (svg==null) return null;
            //chrome
            var res = svg.getElementsByName(name);
            if (res.length>0) return res;
            //firefox
            var node_list = svg.getElementsByTagName("text");
            var res2 = new Array();
            for (var i=0;i<node_list.length;i++){
                var o = node_list[i];
                if  (o.getAttribute("name")==name){
                    res2.push(o);
                }
            }
            //alert(j);
            return res2;
        }
        function setSVG()
        {
            var objs = getOps("ops");
            if (objs==null) return;
            //alert(objs.length);
			for(var i=0;i<objs.length;i++){
				var o=objs[i];
				if (o!=null && selected=="swapNode"){
					o.onclick =  function(){
                            if( this.id){
                                var sid = document.getElementById("inputid").value;
                                document.getElementById(sid).value = this.id;
                            }
						};
					document.getElementById("id1").value="";
					document.getElementById("id2").value="";
				}else if (o!=null && selected=="delNode"){
					o.onclick =  function(){
						var flowID = $("#flowID").val();
						var nodeID = this.id;
						$.post("/alone",
							{flow:flowID,node:nodeID},
							function(res){
								var v = JSON.parse(res);
								if (v.alone==true) {
									if( confirm("是否确定删除该节点？") )
										document.location.href="/del_node?flow="+v.flow+"&node="+v.node;
								}else{
									alert("不可删除，该节点不孤单:"+v.flow+","+v.node);
								}
							});
					}
				}else{
                    if (o!=null) o.onclick = function(){return;}
				}
			}
			document.getElementById("downLink").href = document.getElementById("svg1").src
        }
        function get_name_from_svg(id){
			var element = document.getElementById("svg1");
            var svg = element.getSVGDocument();
            if (svg==null) return;
            return svg.getElementById(id).innerHTML;
		}
        function click_input(o){
			document.getElementById("inputid").value = o.id;
		}
		function loadFlow(){
			var nowTime =new Date().getTime();
			var id = $("#flowID").val();
			var url1 = "/svg?flow="+id+"&t=" + nowTime;
			$("#selFlowID").val(id);
			document.getElementById("svg1").src=url1;
			showOperate(selected);
			$("#selFlowID").change(function(){
				document.location.href="/index?flow="+$("#selFlowID").val();
			});
		}
	function show_opts(name){
        var opts = document.getElementsByName(name);
        var s="id:[";
        for (var i=0;i<opts.length;i++){
            if(i==0)
                s = s + opts[i].value;
            else
                s = s +","+ opts[i].value;
        }
        s=s+"]";
        alert(s);
    }
    function remove_opt(o){
        var p=o.parentNode;
        if (confirm("确定要删除本项？"))
            p.removeChild(o);
    }
    function append_opt(g,id,name){
        var p = document.getElementById(g);
        var opt=document.createElement("option");
        opt.value = id;
        opt.setAttribute("name","opts");
        opt.onclick = function(){
                remove_opt(opt);
            };
        opt.innerHTML=name;
        p.appendChild(opt);
    }
    function clear_opts(name){
        var opts = document.getElementsByName(name);
        for (var i=opts.length-1;i>=0;i--){
            var p=opts[i].parentNode;
            p.removeChild(opts[i]);
        }
    }
    function clearSvgOps(){
        var objs = getOps("ops");
            if (objs==null) return;
            //alert(objs.length);
			for(var i=0;i<objs.length;i++){
				var o=objs[i];
                o.onclick=function(){return;}
            }
    }
    function showOperate(op){
			selected = op;
            var disp = true;
			for(var i=0;i<divs.length;i++){
				var div = document.getElementById(divs[i]);
				if (div.id==op){
					if(div.style.display=="block"){
						div.style.display="none";
                        disp = false;
					}else
						div.style.display="block";
				}else{
					div.style.display="none";
				}
			}
			$("#updateName").val("");
			clear_opts("opts");
            clearSvgOps();
            if(disp) setSVG();
		}
	function updateNode(){
		var id=$("#updateID").val();
		var name=$("#updateName").val();
		var status=$("#updateStatus").val();
		var opts = document.getElementsByName("opts");
        var s="["
        for (var i=0;i<opts.length;i++){
            if(i==0)
                s = s + opts[i].value;
            else
                s = s +","+ opts[i].value;
        }
        s=s+"]";
        var json1 = '{"id":'+id+',"name":"'+name+'","status":'+status+',"depend":'+s+'}';
        $("#updateJSON").val( json1 );
        var form = document.getElementById("formUpdate");
        form.appendChild(document.getElementById("flowID"));
        form.submit();
	}

	function selectDepend(group){
			var element = document.getElementById("svg1");
            var svg = element.getSVGDocument();
            if (svg==null) return;
			//var objs = svg.getElementsByName("ops");
            var objs = getOps("ops");
			for(var i=0;i<objs.length;i++){
				var o=objs[i];
				if(o!=null){
					o.onclick = function(){
							append_opt(group,this.id,this.innerHTML);
						};
				}
			}
		}
		
    function setSwapId(id){
       document.getElementById("inputid").value = id;
    }
	function newNode(){
		var name=$("#newName").val();
		var status=$("#newStatus").val();
		var opts = document.getElementsByName("opts");
        var s="["
        for (var i=0;i<opts.length;i++){
            if(i==0)
                s = s + opts[i].value;
            else
                s = s +","+ opts[i].value;
        }
        s=s+"]";
        var json1 = '{"name":"'+name+'","status":'+status+',"depend":'+s+'}';
        $("#newJSON").val( json1 );
        var form = document.getElementById("formNew");
        form.appendChild(document.getElementById("flowID"));
        form.submit();
	}

	function swapIndex(){
		let v1 = document.getElementById("id1").value;
		let v2 = document.getElementById("id2").value;
		if (v1 === v2) {
			alert("两个节点不能相同！");
			return;
		}
        var form = document.getElementById("formSwap");
        form.appendChild(document.getElementById("flowID"));
        form.submit();
	}
